package com.sandking.builder.factory;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import com.sandking.builder.pojo.Table;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FileWriterFactory {

	private static Configuration cfg;
	public static final int POJO = 1;
	public static final int DAO = 2;
	public static final int IMP = 3;
	public static final int EXT = 4;
	public static final int XML = 5;

	public static Configuration getConfiguration(String url) {
		if (cfg == null) {
			cfg = new Configuration();
			url = FileWriterFactory.class.getResource("/").getPath() + url;
			System.out.println(url);
			File file = new File(url);
			try {
				cfg.setDirectoryForTemplateLoading(file);
				cfg.setObjectWrapper(new DefaultObjectWrapper());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return cfg;
	}

	/**
	 * 
	 * @param cfg
	 *            解析对象
	 * @param templateName
	 *            模板名称
	 * @param root
	 *            数据对象
	 * @param packageName
	 *            包名称
	 * @param fileName
	 *            生成文件名称
	 */
	public static void dataSourceOut(Configuration cfg, String templateName,
			Table table, String fileName, int type) {

		Template temp = null;
		try {
			temp = cfg.getTemplate(templateName);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Writer out = null;
		try {
			String packageName = "";
			switch (type) {
			case POJO:
				packageName = table.getPojoPackage();
				break;
			case DAO:
				packageName = table.getDaoPackage();
				break;
			case IMP:
				packageName = table.getImpPackage();
				break;
			case EXT:
				packageName = table.getExtPackage();
				break;
			case XML:
				packageName = table.getXmlPackage();
				break;
			default:
				packageName = "db.extjs";
			}
			packageName = packageName.replace(".", "/");
			String url = "src/" + packageName + "/" + fileName;
			File file = new File(url);
			out = new FileWriter(file);
			temp.process(table, out);
			temp.process(table, new OutputStreamWriter(System.out));
			out.flush();
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void dataSourceOut(Configuration cfg, String templateName,
			Object root, String fileName) {

		Template temp = null;
		try {
			temp = cfg.getTemplate(templateName);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Writer out = null;
		try {
			if (fileName != null && !"".equals(fileName)) {
				String packageName = "";
				packageName = packageName.replace(".", "/");
				String url = "src/" + packageName + "/" + fileName;
				File file = new File(url);
				out = new FileWriter(file);
				temp.process(root, out);
			}
			temp.process(root, new OutputStreamWriter(System.out));
			out.flush();
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
